#open(CIS,">results_100permutation_cis_numbers") || die "not able to write\n";
#open(TRANS,">results_100permutation_trans_numbers") || die "not able to write\n";
$y =0;
for($i=1e-4;$i> 1e-74;$i = $i/2)
{
	$pval[$y] = $i;
	$y++;
}
@pval = reverse(@pval);
$dir="/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/Matrix_eQTL_R_Source/results_permutation_sorted/";
open(CIS,">/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/Matrix_eQTL_R_Source/results_CIS/$ARGV[0]");
open(TRANS,">/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/Matrix_eQTL_R_Source/results_TRANS/$ARGV[1]");

#$ls = `ls $dir`;
#@a = split("\n",$ls);
#open(WRBUFF,">results_100permutation_cis_trans_numbers") || die "not able to write\n";
#$line = join("\t",@pval);
#print CIS $line."\n";
#print TRANS $line."\n";
#unshift(@a,"eQTL_results_R_cis_sortedpval.txt");
#unshift(@a,"eQTL_results_R_cis.txt");
#for($i=0;$i<@a;$i++)
#{
	#print $a[$i]."\n";
	#if($i != 0)
	#{
	open(BUFF,"$dir/$ARGV[2]") or die "no file exists $ARGV[2]\n";
	#}
	#else
	#{
	#	open(BUFF,"$a[$i]") or die "no file exists $a[$i]\n";	
	#}
	#print "Reading File $a[$i]\n";
	$line = <BUFF>;
	@cis=();
	@trans=();
	for($y=0;$y<@pval;$y++)
	{
		$cis[$y] = 0;
		$trans[$y] = 0;
	}
	while($line=<BUFF>)
	{
		@array = split("\t",$line);
		$val = $array[3];
		$type = $array[5];
		for($y=0;$y<@pval;$y++)
		{
			if($val < $pval[$y])
			{
				if($type == 1)
				{
					$cis[$y]++;	
				}
				else
				{
					$trans[$y]++;	
				}
				last;
			}
		}
	}
	$cis = 0;
	$trans = 0;
	for($y=0;$y<@pval;$y++)
	{
		$cis = $cis+$cis[$y];
		$trans = $trans+$trans[$y];
		#$cis[$y] = $cis;
		#$trans[$y] = $trans[$y];
		#$cis_trans[$y] = $cis."_".$trans;
		$cis_trans1[$y] = $cis;
		$cis_trans2[$y] = $trans;
	}
	$line_cis = join("\t",@cis_trans1);
	$line_trans = join("\t",@cis_trans2);
	#print WRBUFF $line."\n";
	#print $line."\n";
	print CIS $line_cis."\n";
	print TRANS $line_trans."\n";
	#print $line_cis."\n";
        #print $line_trans."\n";
		
#}
#close(WRBUFF);
close(CIS);
close(TRANS);
close(BUFF);
=head	
	$a1=<BUFF>;
	chomp($a1);
	$a1 =~ s/\"//g;
	@a1 = split("\t",$a1);
	$b1=<BUFF>;
	chomp($b1);
	$b1 =~ s/\"//g;
	@b1 = split("\t",$b1);
	print WRBUFF "$a1[0]\t$b1[0]\t$a1[1]\t$b1[1]\n";
=cut
